// pages/login/login.js
var util_md5 = require("../../utils/md5");
var util = require("../../utils/util");
var app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    usePass: true, //使用账号密码登录
    useCode: false, //使用验证码登录
    register: false, //注册
    forgetPass: false, //忘记密码
    showMask: false, //蒙层和获取手机号dialog
    shouquan: true, //授权框显示
    login: false, //登录模块显示
    loginDisabled: true, //登录按钮是否disabled
    phoneClearShow: false, //手机号码清除图标显示
    phoneValue: "", //手机号码value值
    passClearShow: false, //密码清除图标显示
    passValue: "", //密码value值
    repassClearShow: false, //确认密码清除图标显示
    repassValue: "", //确认密码value值
    codeClearShow: false, //验证码清除图标显示
    codeValue: "", //验证码value值
    daojishi: false, //倒计时显示
    initTime: 60, //倒计时初始值
    timer: "", //定时器名称
    countryCode: "中国", //国家区号
    countryName: "86", //国家名称
  },

  /**
   * ————————————微信授权--------------------
   */

  // 授权获取微信信息
  getInfo(e) {
    // console.log(e.detail.errMsg=='getUserInfo:ok');
    if (e.detail.errMsg == "getUserInfo:ok") {
      console.log(e.detail);
      this.setData({
        userData: e.detail.userInfo,
        showMask: true,
      });
    } else {
      console.log("失败");
    }
  },

  // 微信授权获取手机号
  getPhone(e) {
    console.log(e, "3");
    if (e.detail.errMsg == "getPhoneNumber:ok") {
      // 包含encryptedData iv errMsg
      wx.switchTab({
        url: "../index/index",
      });
    } else {
    }
    this.setData({
      showMask: false,
    });
  },

  // 微信授权点击取消获取手机号
  onCancle() {
    this.setData({
      showMask: false,
    });
  },

  /**
   * ————————————表单--------------------
   */

  // 表单提交
  formSubmit(e) {
    var that = this;
    if (that.data.register) {
      // 这是注册
      console.log(e.detail, "注册");
    } else if (that.data.forgetPass) {
      // 忘记、修改密码
      console.log(e.detail, "修改密码");
    } else {
      // 登录
      console.log(e.detail, "登录");
    }
    //表单验证通过，发起请求时清除定时器
    clearInterval(that.data.timer);
    // 表单提交后重置内容、设置button不可点击
    // that.setData({
    //   loginDisabled:true
    // })
  },
  /**
   * ————————————input聚焦--------------------
   */

  // 手机号码聚焦
  phoneFocus(e) {
    if (e.detail.value) {
      console.log(e.detail.value, "聚焦");
      this.setData({
        phoneClearShow: true,
        phoneValue: e.detail.value,
      });
    } else {
    }
    this.checkDisabled();
  },

  // 密码聚焦
  passFocus(e) {
    if (e.detail.value) {
      console.log(e.detail.value, "聚焦");
      this.setData({
        passClearShow: true,
        passValue: e.detail.value,
      });
    } else {
    }
    this.checkDisabled();
  },

  // 确认密码聚焦
  repassFocus(e) {
    if (e.detail.value) {
      console.log(e.detail.value, "聚焦");
      this.setData({
        repassClearShow: true,
        repassValue: e.detail.value,
      });
    } else {
    }
    this.checkDisabled();
  },

  // 验证码聚焦
  codeFocus(e) {
    if (e.detail.value) {
      console.log(e.detail.value, "聚焦");
      this.setData({
        codeClearShow: true,
        codeValue: e.detail.value,
      });
    } else {
    }
    this.checkDisabled();
  },

  /**
   * ————————————input失去焦点--------------------
   */

  // 手机号码失去聚焦
  phoneNoFocus(e) {
    var that = this;
    setTimeout(() => {
      that.setData({
        phoneClearShow: false,
      });
    }, 500);
    this.checkDisabled();
  },

  // 密码失去聚焦
  passNoFocus(e) {
    var that = this;
    setTimeout(() => {
      that.setData({
        passClearShow: false,
      });
    }, 500);
    this.checkDisabled();
  },

  // 确认密码失去聚焦
  repassNoFocus(e) {
    var that = this;
    setTimeout(() => {
      that.setData({
        repassClearShow: false,
      });
    }, 500);
    this.checkDisabled();
  },

  // 验证码失去聚焦
  codeNoFocus(e) {
    var that = this;
    setTimeout(() => {
      that.setData({
        codeClearShow: false,
      });
    }, 500);
    this.checkDisabled();
  },

  /**
   * ————————————input输入--------------------
   */
  // 手机号码输入
  phoneInput(e) {
    if (e.detail.value) {
      console.log(e.detail.value);
      this.setData({
        phoneClearShow: true,
        phoneValue: e.detail.value,
      });
    } else {
      this.setData({
        phoneClearShow: false,
      });
    }
    this.checkDisabled();
  },

  // 密码输入
  passInput(e) {
    if (e.detail.value) {
      console.log(e.detail.value);
      this.setData({
        passClearShow: true,
        passValue: e.detail.value,
      });
    } else {
      this.setData({
        passClearShow: false,
      });
    }
    this.checkDisabled();
  },

  // 确认密码输入
  repassInput(e) {
    if (e.detail.value) {
      console.log(e.detail.value);
      this.setData({
        repassClearShow: true,
        repassValue: e.detail.value,
      });
    } else {
      this.setData({
        repassClearShow: false,
      });
    }
    this.checkDisabled();
  },

  // 验证码输入
  codeInput(e) {
    if (e.detail.value) {
      console.log(e.detail.value);
      this.setData({
        codeClearShow: true,
        codeValue: e.detail.value,
      });
    } else {
      this.setData({
        codeClearShow: false,
      });
    }
    this.checkDisabled();
  },

  /**
   * ————————————input清除--------------------
   */

  // 清除手机号码
  clearPhone() {
    console.log(this.data.phoneValue, "当前");
    this.setData({
      phoneClearShow: false,
      phoneValue: "",
    });
    this.checkDisabled();
  },

  // 清除密码
  clearPass() {
    console.log(this.data.passValue, "当前");
    this.setData({
      passClearShow: false,
      passValue: "",
    });
    this.checkDisabled();
  },

  // 清除确认密码
  reclearPass() {
    console.log(this.data.passValue, "当前");
    this.setData({
      repassClearShow: false,
      repassValue: "",
    });
    this.checkDisabled();
  },

  // 清除验证码
  clearCode() {
    console.log(this.data.codeValue, "当前");
    this.setData({
      codeClearShow: false,
      codeValue: "",
    });
    this.checkDisabled();
  },

  /**
   * ————————————切换表单显示隐藏--------------------
   */
  // 隐藏微信授权、显示登陆

  changeLogin() {
    this.setData({
      shouquan: false, //微信授权模块是否展示
      login: true,
      loginDisabled: true,
    });
    wx.setNavigationBarTitle({
      title: "手机号登录",
    });
  },

  // 使用验证码登录
  useCodeLogin() {
    this.setData({
      usePass: !this.data.usePass,
      useCode: !this.data.useCode,
      loginDisabled: true,
    });
  },

  // 去注册
  gotoRegister() {
    var that = this;
    that.setData({
      register: true,
      usePass: false,
      useCode: false,
      phoneValue: "", //手机号码value值
      passValue: "", //密码value值
      codeValue: "", //验证码value值
      loginDisabled: true,
    });
    wx.setNavigationBarTitle({
      title: "免费注册",
    });
  },

  // 忘记密码
  gotoforgetPass() {
    var that = this;
    that.setData({
      forgetPass: true,
      register: false,
      usePass: false,
      useCode: false,
      phoneValue: "", //手机号码value值
      passValue: "", //密码value值
      codeValue: "", //验证码value值
      loginDisabled: true,
    });
    wx.setNavigationBarTitle({
      title: "忘记密码",
    });
  },

  /**
   * ————————————判断登录按钮disabled--------------------
   */

  //判断登录按钮disabled
  //必填项有数据了，就可以点击，点击之后再判断数据
  checkDisabled() {
    var that = this;
    if (that.data.usePass) {
      //使用账号密码登陆
      // console.log("zhen");
      if (that.data.phoneValue && that.data.passValue) {
        that.setData({
          loginDisabled: false,
        });
      } else {
        that.setData({
          loginDisabled: true,
        });
      }
    } else if (that.data.useCode) {
      //使用验证码登陆
      if (that.data.phoneValue && that.data.codeValue) {
        that.setData({
          loginDisabled: false,
        });
      } else {
        that.setData({
          loginDisabled: true,
        });
      }
    } else if (that.data.register) {
      //注册账号
      if (
        that.data.phoneValue &&
        that.data.codeValue &&
        that.data.passValue &&
        that.data.repassValue &&
        that.data.passValue == that.data.repassValue
      ) {
        that.setData({
          loginDisabled: false,
        });
      } else {
        that.setData({
          loginDisabled: true,
        });
      }
    } else if (that.data.forgetPass) {
      //忘记密码
      if (
        that.data.phoneValue &&
        that.data.codeValue &&
        that.data.passValue &&
        that.data.repassValue &&
        that.data.passValue == that.data.repassValue
      ) {
        that.setData({
          loginDisabled: false,
        });
      } else {
        that.setData({
          loginDisabled: true,
        });
      }
    }
  },

  /**
   * ————————————获取验证码--------------------
   */

  // 点击获取验证码
  receiveCode() {
    var that = this;
    if (that.data.phoneValue) {
      var initTime = that.data.initTime;
      that.setData({
        daojishi: true,
        timer: setInterval(() => {
          initTime--;
          console.log(initTime);
          that.setData({
            initTime: initTime,
          });
          if (initTime == 0) {
            clearInterval(that.data.timer);
            that.setData({
              daojishi: false,
            });
          }
        }, 1000),
      });
    } else {
      wx.showToast({
        title: "请输入手机号",
        icon: "none",
        duration: 2000,
      });
    }
  },
  gotoSelect() {
    app.globalData.countryData = "";
    wx.navigateTo({
      url: "./countryPicker/countryPicker",
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {},

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    console.log(app.globalData.countryData);
    if (app.globalData.countryData == "") {
    } else {
      this.setData({
        countryCode: app.globalData.countryData.code,
        countryName: app.globalData.countryData.name,
      });
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {},
});
